package cn.zhukelili.spark

import org.apache.spark.SparkConf
import org.apache.spark.streaming.kafka.KafkaUtils
import org.apache.spark.streaming.{Seconds, StreamingContext}


/**
  * Spark Streaming对接Kafka的方式
  * exec-memory-kafka_sink
  * 解析系统运行日志
  */
object KafkaReceiverRunningLog {
  def main(args: Array[String]): Unit = {

    if (args.length != 4) {
      System.err.println("Usage: KafkaReceiverWordCount <zkQuorum> <group> <topics> <numThreads>")
      System.exit(1)
    }

    val Array(zkQuorum, group, topics, numThreads) = args

    val sparkConf = new SparkConf()
      .setMaster("local[2]").setAppName("KafkaReceiverRunningLog")

    val ssc = new StreamingContext(sparkConf, Seconds(5))

    val topicMap = topics.split(",").map((_, numThreads.toInt)).toMap

    //TODO... spark streaming如何对接kafka
    val message = KafkaUtils.createStream(ssc, zkQuorum, group, topicMap)

    //    TODO... 自己去测试为什么要取第二个值
    message.map(_._2).flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _).print()
    ssc.start()
    ssc.awaitTermination()

  }
}
